package hu.kisprof.quiz.view.dialogs;

import java.awt.event.ActionEvent;

import javax.swing.JOptionPane;

import hu.kisprof.quiz.model.bean.Question;
import hu.kisprof.quiz.model.bean.Topic;
import hu.kisprof.quiz.view.Labels;
import hu.kisprof.quiz.view.QuizGUI;

public class QuizEditQuestionDialog extends QuizAddQuestionDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3260573905330102591L;

	
	public QuizEditQuestionDialog(QuizGUI gui, Question q) {
		super(gui,q.getId(), q.getQuestion(), q.getBadAnswers(),q.getDifficulty(),q.getGoodAns(), q.getTopics());
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals(Labels.cancel)){
			this.setVisible(false);
		}
		if(e.getActionCommand().equals(Labels.addBadAnswer)){
			badAnswers.add(JOptionPane.showInputDialog(this, Labels.badAnswer));
			refreshBadAnswersList();
		}
		if(e.getActionCommand().equals(Labels.editBadAnswer)){
			
		}
		if(e.getActionCommand().equals(Labels.removeBadAnswer)){
			if(!badAnswersList.isSelectionEmpty())
			badAnswers.remove(badAnswersList.getSelectedValue());
			refreshBadAnswersList();
		}
		
		
		if(e.getActionCommand().equals(Labels.addTopic)){
			Topic tpc = (Topic) JOptionPane.showInputDialog(this, Labels.chooseTopic, Labels.chooseTopic, JOptionPane.PLAIN_MESSAGE, null, gui.getControl().getTopics().toArray(), "topic2");
			if (tpc != null){
				topics.add(tpc);
			}
			refreshTopicList();
		}
		if(e.getActionCommand().equals(Labels.removeTopic)){
			if(!topicsList.isSelectionEmpty()){
				topics.remove(topicsList.getSelectedValue());
				refreshTopicList();
			}
		}
		if(e.getActionCommand().equals(Labels.ok)){
			if(check()){
				gui.getControl().editQuestion(questionId, questionTA.getText(), goodAnsTA.getText(), topics, Integer.parseInt((String)difficultyCB.getSelectedItem()), badAnswers);			
			this.setVisible(false);
			}
		}
	}
	
}
